<template>
    <div>
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="课程名" prop="courseName">
          <el-input v-model="form.courseName"></el-input>
        </el-form-item>
        <el-form-item label="教师" prop="teacherId">
          <el-select v-model="form.teacherId" placeholder="请选择任课教师">
            <el-option :label="teacher.name" :key="teacher.guid" :value="teacher.guid" v-for="teacher in teachers"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择创建时间" prop="createTime">
          <el-col :span="24">
            <el-date-picker type="datetime" placeholder="" v-model="form.createTime"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="即时发布">
          <el-switch v-model="form.delivery"></el-switch>
        </el-form-item>
        <el-form-item label="推荐专业" prop="fitMajor">
          <el-checkbox-group v-model="form.fitMajor">
            <el-checkbox label="IT相关" name="type"></el-checkbox>
            <el-checkbox label="数学相关" name="type"></el-checkbox>
            <el-checkbox label="文学相关" name="type"></el-checkbox>
            <el-checkbox label="金融相关" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="有效标识">
          <el-radio-group v-model="form.validFlag">
            <el-radio :label="0">无效</el-radio>
            <el-radio :label="1">有效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="课程概述" prop="content">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
  export default {
    name: 'Publish',
    data() {
      return {
        teachers:[],
        form: {
          courseName: '',
          teacherId: '',
          createTime: new Date(),
          delivery: true,
          fitMajor: [],
          validFlag: 1,
          content: ''
        },
        rules:{
          courseName: [
            { required: true, message: '课程名不得为空', trigger: 'blur' },
          ],
          teacherId: [
            { required: true, message: '请选择老师', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不得为空', trigger: 'blur' }
          ],
          fitMajor: [
            { required: true, message: '至少选择一个专业推荐', trigger: 'blur' }
          ],
          content:[
            {required: true, message: '课程概述不得为空', trigger: 'blur'}
          ]

        }
      }
    },
    methods: {
      onSubmit() {
        let g=this;
        g.$refs.form.validate(validat=>{
          if (validat){
           let data=JSON.parse(JSON.stringify(g.form));
           g.$set(data,'fitMajor',data.fitMajor.join(";"));
           g.$set(data,'createTime',new Date(data.createTime).getTime());
           g.$ajaxPost('/course/publish',{data:data}).then(res=>{
             g.message({message:'发布成功',type:'success'});
           }).catch(e=>{

           })
          }
        })
      },
      async loadTeachers(){
        return this.$ajaxGet('/teacher/all');
      },
      async init(){
        this.teachers=await this.loadTeachers();
        let permissionCode=this.$store.getters.permissions;
        let g=this;
        if (permissionCode=='2'){//教师端
          g.teachers=g.teachers.filter(data=>data.guid==g.$store.getters.userId);
        }
      }
    },
    created() {
      this.init();
    }
  }
</script>

<style scoped>

</style>
